package cn.ivicar.oss.resource;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.oss.controller.CustomerController;
import cn.ivicar.oss.controller.SimInfoController;
import cn.ivicar.oss.controller.VehicleController;
import cn.ivicar.oss.entity.ResponseEntity;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * Created by Administrator on 2017/5/21.
 */
@Path("vehicle")
@RequestScoped
public class VehicleResource {
    private static transient final Logger LOGGER = LoggerFactory.getLogger(VehicleResource.class);

    @Inject
    private VehicleController vehicleController;
    @Inject
    private CustomerController customerController;
    @Inject
    private SimInfoController simInfoController;

    @Path("data")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getVehicle(@QueryParam("name") String name,
        @QueryParam("value") String value) throws CarsmartException {

        JSONObject vehicles = new JSONObject();
        if (name.equalsIgnoreCase("plate")) {
            vehicles = this.vehicleController.getVehicleByPlate(value);
        } else {
            vehicles = this.vehicleController.getVehicleBySerial(value);
        }

        JSONArray jsonArray = new JSONArray();
        JSONObject result = new JSONObject();
        if (vehicles != null) {
            jsonArray.add(vehicles);
        }
        result.put("data", jsonArray);
        return CacheUtil.noCache(Response.ok(result)).build();

    }

    @Path("datas")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getVehicles() throws CarsmartException {

        JSONArray vehicleList = this.vehicleController.getVehicleList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", vehicleList);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response updateVehicle(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.vehicleController.updateVehicle(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("add")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addVehicle(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.vehicleController.addVehicle(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteVehicle(@QueryParam("vid") String vid) throws CarsmartException {

        ResponseEntity responseEntity = this.vehicleController.deleteVehicle(vid);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("vc")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteVehicleCustomerRel(@QueryParam("vid") String vid,
        @QueryParam("cid") String cid) throws CarsmartException {
        ResponseEntity responseEntity = this.vehicleController.deleteVehicleCustomerRel(vid, cid);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("vs")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteVehicleSimRel(@QueryParam("vid") String vid,
        @QueryParam("iccid") String iccid) throws CarsmartException {
        ResponseEntity responseEntity = this.vehicleController.deleteVehicleSimRel(vid, iccid);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("cvs")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getCVSRelation(@QueryParam("vid") String vid) throws CarsmartException {

        JSONObject content = new JSONObject();
        if (StringUtils.isNotBlank(vid)) {
            JSONObject vehicle = this.vehicleController.getVehicleById(Long.valueOf(vid));
            //暂转为数组
            content.put("vehicle", new String[] {});
            content.put("customer", new String[] {});
            content.put("simInfo", new String[] {});
            if (vehicle != null) {
                String[] v = new String[7];
                v[0] = vehicle.optString("vid");
                v[1] = vehicle.optString("name");
                v[2] = vehicle.optString("serial");
                v[3] = vehicle.optString("plate");
                v[4] = vehicle.optString("vin");
                v[5] = vehicle.optString("ein");
                v[6] = vehicle.optString("created");
                content.put("vehicle", v);
            }

            String iccid = this.vehicleController.getSimIccidByVid(Long.valueOf(vid));

            if (StringUtils.isNotBlank(iccid)) {
                JSONObject simInfo = this.simInfoController.getSimInfoByIccid(iccid);
                //暂转为数组
                if (simInfo != null) {
                    String[] sim = new String[8];
                    sim[0] = simInfo.optString("iccid");
                    sim[1] = simInfo.optString("userCode");
                    sim[2] = simInfo.optString("operator");
                    sim[3] = simInfo.optString("carStatus");
                    sim[4] = simInfo.optString("smsStatus");
                    sim[5] = simInfo.optString("effectTime");
                    sim[6] = simInfo.optString("orgId");
                    sim[7] = simInfo.optString("created");
                    content.put("simInfo", sim);
                }
            }

            Long cid = this.vehicleController.getCustomerIdByVid(Long.valueOf(vid));
            if (cid != null) {
                JSONObject customer = this.customerController.getCustomerById(cid);
                //暂时转为数组
                if (customer != null) {
                    String[] c = new String[7];
                    c[0] = customer.optString("id");
                    c[1] = customer.optString("email");
                    c[2] = customer.optString("mobile");
                    c[3] = customer.optString("nickName");
                    c[4] = customer.optString("sex");
                    c[5] = customer.optString("birthday");
                    c[6] = customer.optString("created");
                    content.put("customer", c);
                }
            }
        }

        return CacheUtil.noCache(Response.ok(content)).build();
    }



    @Path("position")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getVehiclePosition(@QueryParam("vid") String vid) throws CarsmartException {

        ResponseEntity currentPosition = this.vehicleController.getCurrentPosition(vid);
        return CacheUtil.noCache(Response.ok(currentPosition)).build();
    }

    @Path("customers")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getCustomers(@QueryParam("vid") String vid) throws CarsmartException {

        JSONArray customers = this.vehicleController.getCustomers(vid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", customers);
        jsonObject.put("type","vehicleToCustomer");

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    @Path("sims")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSims(@QueryParam("vid") String vid) throws CarsmartException {

        JSONArray simInfos = this.vehicleController.getSims(vid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", simInfos);
        jsonObject.put("type","vehicleToSim");

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    @Path("customer")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteCustomerRel(@QueryParam("vid") String vid,
        @QueryParam("cid") String cid) throws CarsmartException {
        ResponseEntity responseEntity = this.customerController.deleteVehicle(cid,vid);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("sim")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteSimRel(@QueryParam("vid") String vid,
        @QueryParam("iccid") String iccid) throws CarsmartException {
        ResponseEntity responseEntity = this.simInfoController.deleteVehicle(iccid, vid);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("customer")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addCustomerRel(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.vehicleController.addCustomerRel(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("sim")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addSimRel(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.vehicleController.addSimRel(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }


}
